package com.yinhai.util;
import java.util.ArrayList;

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
public class PinYinUtil {

	private final static char[] duoyin = {'重', '藏', '行'}; 
	private final static String[] dyPy = {"chong", "zang", "hang"};
	
	public static final String[] keypad = {"0","1", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz","*","#"};
	
	//得到汉字拼音首字母
    public static String getFirstSpell(String chinese) {
            StringBuffer pybf = new StringBuffer();   
            char[] arr = chinese.toCharArray();   
            HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();   
            defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);   
            defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);   
            boolean isdy = false;
            for (int i = 0; i < arr.length; i++) {   
                    if (arr[i] > 128) { 
                    		isdy = false;
                    		for(int j = 0; j < duoyin.length; ++j) {
                    			if(arr[i] == duoyin[j]) {
                    				pybf.append(dyPy[j].charAt(0));
                    				isdy = true;
                    				break;
                    			}
                    		}
                    		if(isdy)
                    			continue;
                            try {   
                                    String[] temp = PinyinHelper.toHanyuPinyinStringArray(arr[i], defaultFormat);   
                                    if (temp != null) {   
                                            pybf.append(temp[0].charAt(0));   
                                    }   
                            } catch (BadHanyuPinyinOutputFormatCombination e) {   
                                    e.printStackTrace();   
                            } catch(Exception e) {
                            	e.printStackTrace();   
                            }
                    } else {   
                            pybf.append(arr[i]);   
                    }   
            }   
            return pybf.toString().replaceAll("\\W", "").trim();   
    }   
    //得到汉字全拼
    public static String getFullSpell(String chinese) {   
            StringBuffer pybf = new StringBuffer();   
            char[] arr = chinese.toCharArray();   
            HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();   
            defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);   
            defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);   
            boolean isdy = false;
            for (int i = 0; i < arr.length; i++) {   
                    if (arr[i] > 128) {
	                    	isdy = false;
	                		for(int j = 0; j < duoyin.length; ++j) {
	                			if(arr[i] == duoyin[j]) {
	                				pybf.append(dyPy[j]);
	                				isdy = true;
	                				break;
	                			}
	                		}
	                		if(isdy)
	                			continue;
                            try {   
                                String[] array=PinyinHelper.toHanyuPinyinStringArray(arr[i], defaultFormat);
                                if (array!=null&&array.length>0) {
                                    pybf.append(array[0]);   
                                }
                                  
                            } catch (BadHanyuPinyinOutputFormatCombination e) {   
                                    e.printStackTrace();   
                            } catch(Exception e) {
                            	e.printStackTrace();   
                            }
                    } else {   
                            pybf.append(arr[i]);   
                    }   
            }   
            return pybf.toString();   
    }
    //得到汉字到汉字全拼分隔
    public static String getFullSpellSep(String chinese) {   
            StringBuffer pybf = new StringBuffer();   
            char[] arr = chinese.toCharArray();   
            HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();   
            defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);   
            defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);   
            boolean isdy = false;
            for (int i = 0; i < arr.length; i++) {   
                    if (arr[i] > 128) {   
	                    	isdy = false;
	                		for(int j = 0; j < duoyin.length; ++j) {
	                			if(arr[i] == duoyin[j]) {
	                				pybf.append(dyPy[j]);
	                				if(i!=arr.length-1){
	                					pybf.append(',');
	                				}
	                				isdy = true;
	                				break;
	                			}
	                		}
	                		if(isdy)
	                			continue;
                            try {   
                                String[] array=PinyinHelper.toHanyuPinyinStringArray(arr[i], defaultFormat);
                                if (array!=null&&array.length>0) {
                                    pybf.append(array[0]); 
                                    if(i!=arr.length-1){
                                        pybf.append(',');
                                    }
                                }
                           
                            } catch (BadHanyuPinyinOutputFormatCombination e) {   
                                    e.printStackTrace();   
                            } catch(Exception e) {
                            	e.printStackTrace();   
                            }
                    } else {   
                            pybf.append(arr[i]);  
                            if(i!=arr.length-1){
            					pybf.append(',');
            				}
                    }
                    
            }   
            return pybf.toString();   
    }
    //全拼到全拼数字
    public static String getFullSpell2Num(String chinese) {   
        StringBuffer pybf = new StringBuffer();   
        char[] arr = chinese.toCharArray();   
        HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();   
        defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);   
        defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);   
        boolean isdy = false;
        for (int i = 0; i < arr.length; i++) {   
                if (arr[i] > 128) {   
                    	isdy = false;
                		for(int j = 0; j < duoyin.length; ++j) {
                			if(arr[i] == duoyin[j]) {
                				pybf.append(dyPy[j]);
                				isdy = true;
                				break;
                			}
                		}
                		if(isdy)
                			continue;
                        try {   
                                pybf.append(PinyinHelper.toHanyuPinyinStringArray(arr[i], defaultFormat)[0]);   
                        } catch (BadHanyuPinyinOutputFormatCombination e) {   
                                e.printStackTrace();   
                        } catch(Exception e) {
                        	e.printStackTrace();   
                        }  
                } else {   
                        pybf.append(arr[i]);   
                }   
        }   
        return pybf.toString();   
    }
    
    //得到字母对应数字
    public static String getString2Num(String spell){
    	StringBuffer num = new StringBuffer();
    	char[] arr = spell.toCharArray();
    	for(int i=0;i<arr.length;i++){
    		if("abc".indexOf(String.valueOf(arr[i]))> -1){
    			arr[i] = '2';
    		}
    		if("def".indexOf(String.valueOf(arr[i]))> -1){
    			arr[i] = '3';
    		}
    		if("ghi".indexOf(String.valueOf(arr[i]))> -1 ){
    			arr[i] = '4';
    		}
    		if("jkl".indexOf(String.valueOf(arr[i]))> -1 ){
    			arr[i] = '5';
    		}
    		if("mno".indexOf(String.valueOf(arr[i]))> -1 ){
    			arr[i] = '6';
    		}
    		if("pqrs".indexOf(String.valueOf(arr[i]))> -1){
    			arr[i] = '7';
    		}
    		if("tuv".indexOf(String.valueOf(arr[i]))> -1){
    			arr[i] = '8';
    		}
    		if("wxyz".indexOf(String.valueOf(arr[i]))> -1){
    			arr[i] = '9';
    		}
    		num.append(arr[i]);
    	}
    	
    	return num.toString();
    } 
    
    //得到键盘数字对应字母组合
    public static ArrayList<String> allCombinations(String str, String[] keypad) {  
        if (str == null || str.length() == 0) return null;  
        ArrayList<String> list = new ArrayList<String>();  
        String firstCharacter = str.substring(0, 1);  
        String strKeys = keypad[Integer.parseInt(firstCharacter)];  
        if (str.length() == 1) {  
            for (int i = 0; i < strKeys.length(); i++) {  
                list.add(strKeys.substring(i, i+1));  
            }  
            return list;  
        }  
  
        ArrayList<String> list2 = allCombinations(str.substring(1), keypad);  
        for (int i = 0; i < strKeys.length(); i++) {  
            for (String strV : list2) {  
                list.add(strKeys.substring(i, i+1) + strV);  
            }  
        }  
        return list;  
    }
}  

